package com.af.security.auth.login.service;


import com.af.security.mgt.operation.Operation;
import com.af.security.mgt.operation.OperationService;
import com.af.security.mgt.permisson.Permission;
import com.af.security.mgt.permisson.PermissionService;
import com.af.security.mgt.user.User;
import com.af.security.mgt.user.UserService;
import com.af.system.util.AfStringUtils;
import com.szy.system.util.SzyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhenyun.su
 * @commit:
 * @since : 2020/03/25
 */

@Service
public class AfAuthDataService {
    private static final Logger logger = LoggerFactory.getLogger(AfAuthDataService.class);

    @Autowired
    private UserService userService;
    @Autowired
    private OperationService operationService;
    @Autowired
    private PermissionService permissionService;

    public User getUserFromPhone(String phone){
        return userService.findByPhone(phone);
    }
    public User getUserFromMail(String mail){
        return userService.findByEmail(mail);
    }
    public User getUserBySourceId(String sourceId) {
        return userService.findBySourceId(sourceId);
    }
    public User getUserByUsernameOrEmailOrPhone(String name){
        return userService.findByUsernameOrEmailOrPhone(name);
    }

    public User saveAccount(User user) {
        return userService.save(user);
    }

    /**
     * @comment : 获取用户信息
     */
    public User getUserInfo(String username){
        logger.debug("getUserInfo, according to {}",username);
        return userService.findByUsername(username);
    }

    /**
     * @comment : 获取所有权限 - Permission
     */
    public List<Permission> getAllPermission(Long userId){
        List<Permission> permissions = this.permissionService.getPermitByUserId(userId);
        for (Permission permission : permissions) {
            String operationIds = StringUtils.trimWhitespace(permission.getOperationIds());
            if (StringUtils.hasText(operationIds)) {
                if ("all".equalsIgnoreCase(permission.getOperationIds())){
                    permission.setOperations(operationService.getAll());
                }else{
                    permission.setOperations(operationService.getOperationsByIds(operationIds));
                }
            }
        }
        return permissions;
    }

    /**
     * @comment : 获取所有操作权限，格式：permission:operation
     */
    public List<String> getAllOperateCode(List<Permission> permissions){
        List<String> operateCodes = new ArrayList<>();
        for (Permission permission : permissions) {
            String identity =  AfStringUtils.emptyString(permission.getPermission());
            if (!ObjectUtils.isEmpty(permission.getOperations())){
                for(Operation operation: permission.getOperations()){
                    if (StringUtils.isEmpty(operation.getPermission())){
                        continue;
                    }
                    if (!StringUtils.isEmpty(identity)) {
                        operateCodes.add(StringUtils.trimWhitespace(identity+":"+operation.getPermission()));
                    }else{
                        operateCodes.add(StringUtils.trimWhitespace(operation.getPermission()));
                    }
                }
            }
        }
        return operateCodes;
    }

}
